package org.study.apitest;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.study.mapper.UserFormateMapper;

import java.io.IOException;
import java.io.InputStream;

/**
 * @program: mybatis-demo-study
 * @description:  入门demo
 * @author: tmz
 * @create: 2020-08-21 11:44
 */
public class FirstDemoOfSelectOne {
    public static void main(String[] args) {
        InputStream resourceAsStream = null;
        try{
            resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
            try( SqlSession sqlSession = factory.openSession()){
                UserFormateMapper mapper = sqlSession.getMapper(UserFormateMapper.class);
                System.out.println(mapper.selectOne(1));
                /*UserFormate userFormate = sqlSession.selectOne("org.study.mapper.UserFormateMapper.selectOne");
                System.out.println(userFormate);*/
            }catch (Exception e){
                e.printStackTrace();
            }

        }catch (Exception e) {
            System.out.println("异常....");
            e.printStackTrace();
        }finally {
            if(resourceAsStream!=null){
                try {
                    resourceAsStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("结束");
        }
    }
}
